package com.example.smartcity.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smartcity.R;
import com.example.smartcity.bean.GarbageNews;
import com.example.smartcity.bean.GarbageNewsBean;
import com.example.smartcity.bean.NewsBean;
import com.example.smartcity.fragment.NShizhengFragment;
import com.example.smartcity.utils.APIConfig;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class GarbageNewsActivity extends AppCompatActivity {
    private Context context;
    ListView lv_garbage_news;
    GarbageNewsBean garbageNews;
    GarbageNews garbageNewsBean;
    List<GarbageNewsBean.NewsItem> grbageNewsList;
    GarbageNewsAdapter garbageNewsAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this.getApplicationContext();
        setContentView(R.layout.activity_garbage_news);
        lv_garbage_news = findViewById(R.id.lv_garbage_news);
        lv_garbage_news.setAdapter(garbageNewsAdapter);
        initData();

    }
    private void initData() {
        Log.i("getNewsData"," Activated getGarbageNewsData");
        OkHttpClient client = new OkHttpClient();
        Request request  = new Request.Builder()
                .url("https://apis.tianapi.com/lajifenleinews/index?key=5ce9c93a896acbb81c8dfea083343944&num=10" )
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("onFailure",e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    String result = response.body().string().toString();

                    Log.i("News_Request_Success:",result);
                    Gson gson = new Gson();

                    garbageNews = gson.fromJson(result, GarbageNewsBean.class);
                    //rowsBeans = gson.fromJson(newsBean.getRows(), listType);
                    Log.i("szrowsBeans",garbageNews.getMsg());
                    Log.i("resultlist",garbageNews.getResult().getList()+"");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            GarbageNewsAdapter newsAdapter = new GarbageNewsAdapter(context,garbageNews.getResult().getList());
                            lv_garbage_news.setAdapter(newsAdapter);

                            lv_garbage_news.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                    Intent intent = new Intent(GarbageNewsActivity.this,WebViewActivity.class);
                                    ImageView new_img = view.findViewById(R.id.new_img);
                                    new_img.setDrawingCacheEnabled(Boolean.TRUE);
                                    intent.putExtra("url",garbageNewsBean.getSource());
                                    startActivity(intent);
                                }
                            });

                        }
                    });
                }
            }
        });

    }



    public class GarbageNewsAdapter extends BaseAdapter {
        private Context context;
        private List<GarbageNewsBean.NewsItem> garbageNewsList;
        private LayoutInflater inflater;

        public GarbageNewsAdapter(Context context, List<GarbageNewsBean.NewsItem> garbageNewsList) {
            this.context = context;
            this.garbageNewsList = garbageNewsList;
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            return garbageNewsList.size();
        }

        @Override
        public Object getItem(int position) {
            return garbageNewsList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.item_garbage_news, parent, false);
                viewHolder = new ViewHolder();
                viewHolder.titleTextView = convertView.findViewById(R.id.tv_garbage_news_title);
                viewHolder.contentTextView = convertView.findViewById(R.id.tv_garbage_news_content);
                viewHolder.ctimeTextView = convertView.findViewById(R.id.tv_garbage_news_ctime);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            GarbageNewsBean.NewsItem garbageNews = garbageNewsList.get(position);
            viewHolder.titleTextView.setText(cutContext12(garbageNews.getTitle()));
            viewHolder.contentTextView.setText(garbageNews.getDescription());
            viewHolder.ctimeTextView.setText(garbageNews.getCtime());

            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, WebViewActivity.class);
                    Log.i("yrl",garbageNewsList.get(position).getUrl());
                    intent.putExtra("url", garbageNewsList.get(position).getUrl());
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);
                }
            });

            return convertView;
        }

        class ViewHolder {
            TextView titleTextView;
            TextView contentTextView;
            TextView ctimeTextView;
        }
    }
    private String cutContext12(String str) {
        if (str.length() > 12){
            String str1 = str.substring(0, 12) + "...";
            return str1;
        } else {
            return str;
        }
    }
}